/**
 * 
 * @author andipandi
 *
 */

public class Segment {
	
	/**
	 * constants
	 */
	public static final int SEG_SIZE 		= 110;
	public static final int MAX_SEGMENTS	= 0x400;
	public static final int SEG_MASK		= 0x3FF;
	public static final char SEG_TYPE_FIRST = 0;
	public static final char SEG_TYPE_LAST 	= 0xFF;
	public static final char SEG_TYPE_MID 	= 0xF0;
	public static final char SEG_TYPE_DUMMY	= 0x0F;
	
	
	/**
	 * attributes
	 */
	int nr;
	char type;
	byte[] data;
	
	/**
	 * 
	 * @param sequence_nr
	 * @param buf
	 * @param pos
	 * @param len
	 */
	public Segment(int sequence_nr, char segment_type, byte[] buf, int pos, int len){
		
		nr = sequence_nr;
		type = segment_type;
		data = new byte[len];
		for(int i=0; i<len; i++)
			data[i] = buf[i+pos];
	}
	
	/**
	 * 
	 * @param snr sequence number of new segment
	 * @return number of segments missing between last written segment and snr
	 */
	public static int distance(int from, int to){
		
		if(from<=to)
			return to - from;
		
		return MAX_SEGMENTS - from + to;
	}
}
